#ifndef __INF_SOLENOIDVALVE_H__
#define __INF_SOLENOIDVALVE_H__

#include "Common_Config.h"
#include "Driver_ATIM.h"
#include "Driver_GPTIM.h"
#include "Driver_LPTIM32.h"
#include "FreeRTOS.h"
#include "Inf_74HC595.h"
#include "Inf_BTS7020.h"
#include "task.h"

#include <stdbool.h>

typedef void (*SetChannelDutyAndStateFunc)(void *, uint32_t channel, bool isComplementary,
                                           uint8_t dutyCycle, bool isEnable);

/* 电磁阀类型 */
typedef enum
{
    SOLENOID_VALVE_INDEX_AM1,
    SOLENOID_VALVE_INDEX_AM2,
    SOLENOID_VALVE_INDEX_AM3,
    SOLENOID_VALVE_INDEX_AM4,
    SOLENOID_VALVE_INDEX_AM5,
    SOLENOID_VALVE_INDEX_AM6,
    SOLENOID_VALVE_INDEX_AM7,
    SOLENOID_VALVE_INDEX_AM8,
    SOLENOID_VALVE_INDEX_AM9,
    SOLENOID_VALVE_INDEX_AM10,
    SOLENOID_VALVE_INDEX_YT1,
    SOLENOID_VALVE_INDEX_YTR1,
    SOLENOID_VALVE_INDEX_YT2,
    SOLENOID_VALVE_INDEX_YTR2,
    SOLENOID_VALVE_INDEX_YT3,
    SOLENOID_VALVE_INDEX_YTR3,
    SOLENOID_VALVE_INDEX_YT4,
    SOLENOID_VALVE_INDEX_YTR4,
    SOLENOID_VALVE_INDEX_YT5,
    SOLENOID_VALVE_INDEX_YTR5,
    SOLENOID_VALVE_INDEX_PASSIVE_LEFT_YTR6,  // 靠背侧翼
    SOLENOID_VALVE_INDEX_PASSIVE_RIGHT_YTR7, // 坐垫侧翼
    SOLENOID_VALVE_INDEX_MAX
} SolenoidValvIndex_e;

/* 电磁阀类型 */
typedef enum
{
    SOLENOID_VALVE_TYPE_AM1,
    SOLENOID_VALVE_TYPE_AM2,
    SOLENOID_VALVE_TYPE_AM3,
    SOLENOID_VALVE_TYPE_AM4,
    SOLENOID_VALVE_TYPE_AM5,
    SOLENOID_VALVE_TYPE_AM6,
    SOLENOID_VALVE_TYPE_AM7,
    SOLENOID_VALVE_TYPE_AM8,
    SOLENOID_VALVE_TYPE_AM9,
    SOLENOID_VALVE_TYPE_AM10,
    SOLENOID_VALVE_TYPE_YT1,
    SOLENOID_VALVE_TYPE_YTR1,
    SOLENOID_VALVE_TYPE_YT2,
    SOLENOID_VALVE_TYPE_YTR2,
    SOLENOID_VALVE_TYPE_YT3,
    SOLENOID_VALVE_TYPE_YTR3,
    SOLENOID_VALVE_TYPE_YT4,
    SOLENOID_VALVE_TYPE_YTR4,
    SOLENOID_VALVE_TYPE_YT5,
    SOLENOID_VALVE_TYPE_YTR5,
    SOLENOID_VALVE_TYPE_PASSIVE_LEFT_YTR6,  // 靠背侧翼
    SOLENOID_VALVE_TYPE_PASSIVE_RIGHT_YTR7, // 坐垫侧翼
    SOLENOID_VALVE_TYPE_MAX
} SolenoidValveType_e;

/* 电磁阀使用类型 */
typedef enum
{
    SOLENOID_VALVE_USE_MASSAGE,
    SOLENOID_VALVE_USE_FLANK,
    SOLENOID_VALVE_USE_LUMBAR_SUPPORT,
    SOLENOID_VALVE_USE_MAX
} SolenoidValveUse_e;

typedef enum
{
    TIMER_TYPE_ATIM,
    TIMER_TYPE_GPTIM,
    TIMER_TYPE_LPTIM32,
    TIMER_TYPE_NONE
} TimerType_e;

/* 电磁阀 */
typedef struct
{
    /* 电磁阀类型 */
    SolenoidValveType_e solenoidValveType;

    /* 电磁阀用途类型 */
    SolenoidValveUse_e solenoidValveUse;

    /* 占空比 */
    uint8_t dutyCycle;

    /* 阀是否使能 */
    bool isEnable;

    /* 定时器类型 */
    TimerType_e timerType;

    /* 定时器种类 */
    union
    {
        ATIM_Type *atim;

        GPTIM_Type *gptim;

        LPTIM32_Type *lptim32;
    } Timer;

    /* 是否是互补通道 */
    bool isComplementary;

    /* 定时器通道号 */
    uint32_t timerChannel;

    /* 电磁阀GPIO端口 */
    GPIO_Type *port;

    /* 电磁阀GPIO引脚号 */
    uint32_t pin;

    /* 设置电磁阀状态和占空比的函数指针 */
    SetChannelDutyAndStateFunc setChannelDutyAndStateFunc;

} SolenoidValve;

extern SolenoidValve solenoidValveList[];

/**
 * @brief 初始化电磁阀
 *
 */
void Inf_SolenoidValve_Init(void);

/**
 * @brief 使能电磁阀开关
 *
 */
void Inf_SolenoidValve_EnableSwitch(void);

/**
 * @brief 失能电磁阀
 *
 */
void Inf_SolenoidValve_DisableSwitch(void);

/**
 * @brief 激活指定的电磁阀
 *
 * @param solenoidValve 阀指针
 */
void Inf_SolenoidValve_ActivateSolenoidValve(SolenoidValve *solenoidValve);

/**
 * @brief 禁用指定的电磁阀
 *
 * @param solenoidValve 阀指针
 */
void Inf_SolenoidValve_DeactivateSolenoidValve(SolenoidValve *solenoidValve);

/**
 * @brief 同时激活多个电磁阀（避免延时导致的滞后问题）
 *
 * @param valveIndexes 阀门索引数组
 * @param count 阀门数量
 */
void Inf_SolenoidValve_ActivateMultiValves(const uint8_t *valveIndexes, uint8_t count);

/**
 * @brief 同时禁用多个电磁阀
 *
 * @param valveIndexes 阀门索引数组
 * @param count 阀门数量
 */
void Inf_SolenoidValve_DeactivateMultiValves(const uint8_t *valveIndexes, uint8_t count);

#endif
